package dimension.starry.loot;

import dimension.starry.utils.RandomUtil;
import net.minecraft.item.Item;
import net.minecraft.item.Items;

import java.util.HashMap;
import java.util.Map;

public class PastureLoots {

    private static final Map<Item, Integer> LOOT_WEIGHT_MAP = new HashMap<>() {{
        put(Items.AIR, 9000);
        put(Items.BEEF, 100);
        put(Items.BONE, 100);
        put(Items.CHICKEN, 100);
        put(Items.COBBLESTONE, 100);
        put(Items.DIRT, 300);
        put(Items.EGG, 100);
        put(Items.FEATHER, 500);
        put(Items.HONEY_BOTTLE, 100);
        put(Items.LADDER, 100);
        put(Items.LEATHER, 100);
        put(Items.RABBIT_HIDE, 100);
        put(Items.RABBIT_STEW, 100);
        put(Items.RABBIT_FOOT, 50);
        put(Items.WHITE_WOOL, 100);
        put(Items.IRON_HORSE_ARMOR, 100);
        put(Items.LEATHER_HORSE_ARMOR, 100);
        put(Items.GOLDEN_HORSE_ARMOR, 50);
        put(Items.SADDLE, 50);
        put(Items.LEAD, 50);
        put(Items.NAME_TAG, 10);
        put(Items.MILK_BUCKET, 30);
        put(Items.PORKCHOP, 100);
        put(Items.RABBIT, 100);
        put(Items.SHEARS, 50);
        put(Items.STICK, 500);
    }};

    public static Item getRandomItem() {
        int totalWeight = LOOT_WEIGHT_MAP.values().stream().mapToInt(i -> i).sum();
        int random = RandomUtil.randomInt(1, totalWeight + 1), current = 0;
        for (Map.Entry<Item, Integer> entry : LOOT_WEIGHT_MAP.entrySet()) {
            if (random > current && random <= current + entry.getValue()) {
                return entry.getKey();
            }
            current += entry.getValue();
        }
        throw new RuntimeException(String.format("calculate error, weight not in range: total=%d random=%d current=%d",
            totalWeight,
            random,
            current
        ));
    }

}
